/*
 * 版权所有 ( c ) 联想集团有限公司 2005-2009 保留所有权利。
 * 项目：	Lenovo Push Service
 * 文件名：	ErrorInfo.java
 * 版本:    1.0
 * 描述：	
 * 作者：	刘兵召
 * 日期：	Jun 8, 2010 
 * 修改历史：
 * 【时间】		【修改者】	【修改内容】
 */
package com.feng.util.xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;

import com.feng.util.StringUtil;

public class ErrorInfo implements XmlSerializable,Serializable {
	private static final long serialVersionUID = 1L;
	private static final String XML_FILE = "E:/code/MyTest/src/com/feng/util/xml/error.xml";
	private String Code;
	private String Timestamp;
	private String Message;
	private String Detail;
	private String Source;
	private String URL;
	
	public String getCode() {
		return Code;
	}

	public void setCode(String code) {
		Code = code;
	}

	public String getTimestamp() {
		return Timestamp;
	}

	public void setTimestamp(String timestamp) {
		Timestamp = timestamp;
	}

	public String getMessage() {
		return Message;
	}

	public void setMessage(String message) {
		Message = message;
	}

	public String getDetail() {
		return Detail;
	}

	public void setDetail(String detail) {
		Detail = detail;
	}

	public String getSource() {
		return Source;
	}

	public void setSource(String source) {
		Source = source;
	}

	public String getURL() {
		return URL;
	}

	public void setURL(String url) {
		URL = url;
	}

	public static ErrorInfo readFrom(InputStream inStream) throws Exception {
		ErrorInfo result = new ErrorInfo();
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser saxParser = spf.newSAXParser();
		saxParser.parse(inStream, new ErrorInfoSAXHander(result));
		return result;
	}
	
	public static void main(String[] args) throws FileNotFoundException, Exception{
		ErrorInfo errorInfo = readFrom(new FileInputStream(XML_FILE));
		System.out.println(errorInfo.toString());
		System.out.println(errorInfo.getMessage());
		System.out.println(errorInfo.toXML());
	}
	@Override
	public String toString() {
		return StringUtil.toStringBuilder(this);
	}

	public String toXML() {
		Document doc = DocumentHelper.createDocument();
		doc.setXMLEncoding("UTF-8");
		Element serviceElem = new DOMElement("Error");

		serviceElem.addElement(ErrorInfoSAXHander.Code).setText(Code == null ? "" : Code);
		serviceElem.addElement(ErrorInfoSAXHander.URL).setText(URL == null ? "" : URL);
		doc.add(serviceElem);
		return doc.asXML();
	}
}